/*
 * @(#)LoginView.java		       Project:SpikeSC
 * Date:2012-7-24
 *
 * Copyright (c) 2011 CFuture09, Institute of Software, 
 * Guangdong Ocean University, Zhanjiang, GuangDong, China.
 * All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.sinaapp.msdxblog.spikesc.ui;

import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;

import org.apache.log4j.PropertyConfigurator;
import org.dyno.visual.swing.layouts.Constraints;
import org.dyno.visual.swing.layouts.GroupLayout;
import org.dyno.visual.swing.layouts.Leading;

import com.sinaapp.msdxblog.spikesc.logic.UserLogic;

//VS4E -- DO NOT REMOVE THIS LINE!
/**
 * @author Geek_Soledad (66704238@51uc.com)
 */
public class LoginView extends JFrame {

	private static final long serialVersionUID = 1L;
	private JLabel jLUsername;
	private JLabel jLPassword;
	private JPasswordField jPasswordField;
	private JTextField jTFName;
	private JButton jBLogin;
	private JLabel jLNameTip;
	private JLabel jLPasswordTip;
	private static final String PREFERRED_LOOK_AND_FEEL = "com.sun.java.swing.plaf.windows.WindowsLookAndFeel";
	public LoginView() {
		initComponents();
	}

	private void initComponents() {
		setTitle("登录");
		setLayout(new GroupLayout());
		add(getJLabel0(), new Constraints(new Leading(37, 10, 10), new Leading(30, 10, 10)));
		add(getJTFName(), new Constraints(new Leading(100, 174, 10, 10), new Leading(27, 12, 12)));
		add(getJLPassowrd(), new Constraints(new Leading(37, 12, 12), new Leading(90, 10, 10)));
		add(getJPasswordField0(), new Constraints(new Leading(100, 174, 12, 12), new Leading(87, 12, 12)));
		add(getJBLogin(), new Constraints(new Leading(123, 10, 10), new Leading(139, 10, 10)));
		add(getJLNameTip(), new Constraints(new Leading(100, 174, 12, 12), new Leading(57, 12, 12)));
		add(getJLPasswordTip(), new Constraints(new Leading(100, 174, 12, 12), new Leading(114, 10, 10)));
		setSize(320, 180);
	}

	private JLabel getJLPasswordTip() {
		if (jLPasswordTip == null) {
			jLPasswordTip = new JLabel();
		}
		return jLPasswordTip;
	}

	private JLabel getJLNameTip() {
		if (jLNameTip == null) {
			jLNameTip = new JLabel();
		}
		return jLNameTip;
	}

	private JButton getJBLogin() {
		if (jBLogin == null) {
			jBLogin = new JButton();
			jBLogin.setText("登录");
			jBLogin.addMouseListener(new MouseAdapter() {
	
				public void mouseClicked(MouseEvent event) {
					jBLoginMouseMouseClicked(event);
				}
			});
		}
		return jBLogin;
	}

	private JTextField getJTFName() {
		if (jTFName == null) {
			jTFName = new JTextField();
			jTFName.addFocusListener(new FocusAdapter() {
	
				public void focusLost(FocusEvent event) {
					jTFNameFocusFocusLost(event);
				}
			});
		}
		return jTFName;
	}

	private JPasswordField getJPasswordField0() {
		if (jPasswordField == null) {
			jPasswordField = new JPasswordField();
			jPasswordField.addFocusListener(new FocusAdapter() {
	
				public void focusLost(FocusEvent event) {
					jPasswordFieldFocusFocusLost(event);
				}
			});
		}
		return jPasswordField;
	}

	private JLabel getJLPassowrd() {
		if (jLPassword == null) {
			jLPassword = new JLabel();
			jLPassword.setText("密  码");
		}
		return jLPassword;
	}

	private JLabel getJLabel0() {
		if (jLUsername == null) {
			jLUsername = new JLabel();
			jLUsername.setText("账  号");
		}
		return jLUsername;
	}

	private static void installLnF() {
		try {
			String lnfClassname = PREFERRED_LOOK_AND_FEEL;
//			if (lnfClassname == null)
//				lnfClassname = UIManager.getCrossPlatformLookAndFeelClassName();
			UIManager.setLookAndFeel(lnfClassname);
		} catch (Exception e) {
			System.err.println("Cannot install " + PREFERRED_LOOK_AND_FEEL
					+ " on this platform:" + e.getMessage());
		}
	}

	/**
	 * Main entry of the class.
	 * Note: This class is only created so that you can easily preview the result at runtime.
	 * It is not expected to be managed by the designer.
	 * You can modify it as you like.
	 */
	public static void main(String[] args) {
		PropertyConfigurator.configure("spikesc-log4j.properties");
		installLnF();
		SwingUtilities.invokeLater(new Runnable() {
			@Override
			public void run() {
				LoginView frame = new LoginView();
				frame.setDefaultCloseOperation(LoginView.EXIT_ON_CLOSE);
				frame.setTitle("登录");
				frame.getContentPane().setPreferredSize(frame.getSize());
				frame.pack();
				frame.setLocationRelativeTo(null);
				frame.setVisible(true);
			}
		});
	}

	private void jBLoginMouseMouseClicked(MouseEvent event) {
		if (checkUsername() && checkPassword()) {
			if (new UserLogic().login(jTFName.getText(), jPasswordField.getPassword())) {
				// TODO 登录成功;
				System.out.println("登录成功");
			}
			else {
				JOptionPane.showMessageDialog(this, "用户名或密码错误！");
			}
		}
	}

	private void jTFNameFocusFocusLost(FocusEvent event) {
		checkUsername();
	}

	private boolean checkUsername() {
		String name = jTFName.getText();
		if (name== null || name.equals("")) {
			jLNameTip.setText("请输入账号");
			return false;
		} else {
			jLNameTip.setText("");
			return true;
		}
	}

	
	private void jPasswordFieldFocusFocusLost(FocusEvent event) {
		checkPassword();
	}

	private boolean checkPassword() {
		String password = new String(jPasswordField.getPassword());
		if (password== null || password.equals("")) {
			jLPasswordTip.setText("请输入密码");
			return false;
		} else {
			jLPasswordTip.setText("");
			return true;
		}
	}

}
